#ifndef _SOCK_HPP_
#define _SOCK_HPP_

#include <iostream>
#include <string>
#include <cstring>
#include <cstdbool>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

namespace ns_sock
{
    class Sock
    {
    private:
        const static int gbacklog = 20;
    public:
        Sock() {}
        ~Sock() {}

        // 创建网络套接字
        int Socket()
        {
            int sockfd = socket(AF_INET, SOCK_STREAM, 0);
            if (sockfd < 0)
            {
                // std::cerr << "Sock::Socket error" << std::endl;
                exit(1);
                return -1;
            }
            return sockfd;
        }

        // 将sockfd,port,ip进行绑定
        bool Bind(const int &sockfd, const uint16_t &port, const std::string &ip = "0.0.0.0")
        {
            struct sockaddr_in local;

            memset((void *)&local, 0, sizeof(local));

            local.sin_family = AF_INET;
            local.sin_port = htons(port);
            local.sin_addr.s_addr = inet_addr(ip.c_str());

            if (bind(sockfd, (struct sockaddr *)&local, sizeof(local)) < 0)
            {
                // std::cerr << "Sock::Bind error" << std::endl;
                exit(2);
                return false;
            }
            return true;
        }

        // 将sockfd设置为监听状态
        bool Listen(int listensock)
        {
            if (listen(listensock, gbacklog) < 0)
            {
                // std::cerr << "Sock::Listen error" << std::endl;
                exit(3);
                return false;
            }
            return true;
        }

        // 获取连接
        int Accept(int listensock, uint16_t *port, std::string *ip)
        {
            struct sockaddr_in service;
            socklen_t servicelen = sizeof(service);
            memset(&service, 0, servicelen);

            int servicesock = accept(listensock, (struct sockaddr *)&service, &servicelen);
            if (servicesock < 0)
            {
                // std::cerr << "Sock::Accept error" << std::endl;
                exit(4);
                return -1;
            }

            if (port != nullptr)
                *port = ntohs(service.sin_port);
            if (ip != nullptr)
                *ip = std::string(inet_ntoa(service.sin_addr));

            return servicesock;
        }

        // 申请连接
        bool Connect(int sockfd, const uint16_t &port, const std::string &ip)
        {
            struct sockaddr_in server;
            memset(&server, 0, sizeof(server));

            server.sin_family = AF_INET;
            server.sin_port = htons(port);
            server.sin_addr.s_addr = inet_addr(ip.c_str());

            if (connect(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
            {
                // std::cerr << "Sock::Connect error" << std::endl;
                exit(5);
                return false;
            }

            return true;
        }
    };
    // const int TcpServer::gbacklog;
}

#endif